package com.wujilee.payservice.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.partnerpayments.nativepay.NativePayService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liwuji
 * @date 2024/6/17
 * @desc
 **/
@Configuration
@ConfigurationProperties("pay.weixin")
@Data
public class WeixinPayConfig {
    /** 商户号 */
    public  String merchantId = "190000****";
    /** 商户API私钥路径 */
    public  String privateKeyPath = "/Users/yourname/your/path/apiclient_key.pem";
    /** 商户证书序列号 */
    public  String merchantSerialNumber = "5157F09EFDC096DE15EBE81A47057A72********";
    /** 商户APIV3密钥 */
    public  String apiV3Key = "...";
    /**
     * appid
     */
    private String appid;
    /**
     * 支付回调
     */
    private String notifyUrl;
    
    @Bean
    public Config weixinConfig(){
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        return new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
    }
    @Bean
    public NativePayService nativePayService(Config config){
        return new NativePayService.Builder().config(config).build();
    }
    @Bean
    public NotificationConfig notificationConfig(){
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
    }
    
    @Bean
    public NotificationParser notificationParser(NotificationConfig notificationConfig){
        return new NotificationParser(notificationConfig);
    }
    
    
}
